package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.entity.content.Button;
import cn.livingCloud.entity.system.Setting;
import cn.livingCloud.repository.SettingMapper;
import cn.livingCloud.wechat.entity.Menu;
import cn.livingCloud.wechat.handler.WechatMenuHandler;
import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * 推送更新
 * Created by DingJie  on 2016/1/7.
 */
public class MenuPusher extends ButtonTreeLoader {
    private static Logger LOG = Logger.getLogger(MenuPusher.class);
    private transient SettingMapper settingMapper = BeanProvider.getBean(SettingMapper.class);

    public boolean push() {
        boolean result = false;
        List<Button> parentList = super.loadTree();

        Menu menu = new Menu();
        menu.getButton().addAll(parentList);
        String s = JSON.toJSONString(menu);

        WechatMenuHandler handler = new WechatMenuHandler();
        Setting setting = settingMapper.findSetting();
        if (setting != null) {
            result = handler.createMenu(s, setting.getAppid(), setting.getSecret());
            LOG.info("推送更新到微信返回:" + result);
        } else {
            LOG.info("未配置appid,secret 无法推送菜单");
        }
        return result;
    }

}
